package com.health.db.service.msg.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.health.commons.PropertiesService;
import com.health.db.dao.msg.IIMsgIMMessageDao;
import com.health.db.model.IMsgIMMessage;
import com.health.db.model.IMsgIMMessageExt;
import com.health.db.service.msg.IIMsgIMMessageService;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.GetRemoteFileHTTPClient;
import com.health.mapp.utils.MappGlobal;
import com.health.mapp.utils.RongCloudMessageUtil;
import com.health.mapp.utils.UploadByOss;

/**
 * Created by helili on 2017/3/21.
 */
public class IMsgIMMessageServiceImpl implements IIMsgIMMessageService {
	private static Logger logger = Logger.getLogger(IMsgIMMessageServiceImpl.class);
    private IIMsgIMMessageDao iMsgIMMessageDao;

	@Override
	public int deleteByPrimaryKey(Long recid) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(IMsgIMMessage record) {
		// TODO Auto-generated method stub
		return iMsgIMMessageDao.insert(record);
	}

	 

	@Override
	public IMsgIMMessage selectByPrimaryKey(Long recid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKey(IMsgIMMessage record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<IMsgIMMessage> selectByCondition(IMsgIMMessageExt example) {
		// TODO Auto-generated method stub
		return iMsgIMMessageDao.selectByCondition(example);
	}

	public IIMsgIMMessageDao getiMsgIMMessageDao() {
		return iMsgIMMessageDao;
	}

	public void setiMsgIMMessageDao(IIMsgIMMessageDao iMsgIMMessageDao) {
		this.iMsgIMMessageDao = iMsgIMMessageDao;
	}

	@Override
	public void insertBatch(List<IMsgIMMessage> record) {
		iMsgIMMessageDao.insertBatch(record);
	}

	@Override
	public int updateOSSInfoByPrimaryKey(List<IMsgIMMessage> messageList) {
		
		//root path
		String webRootPath=MappGlobal.getInstance().getWebRootPath();
		 
		List<IMsgIMMessage> messageArray=  new ArrayList<IMsgIMMessage>();
		for(int i=0;i<messageList.size();i++)
		{
			IMsgIMMessage message = messageList.get(i);
			String mediatype=message.getMediatype();
			String groupId=String.valueOf(message.getGroupid());
			
			// classify path
			String classifyPath="";
			if("RC:VcMsg".equals(mediatype))
			{
				
				classifyPath= PropertiesService.getString("upload.VcMsg.path");
			}
			else if("RC:ImgMsg".equals(mediatype))
			{
				classifyPath= PropertiesService.getString("upload.ImgMsg.path");
			}
			 
			String savePath=webRootPath+groupId+"/"+classifyPath;
			File tempFp = new File(savePath);
			// 创建目录
			if (!tempFp.exists()) {
				tempFp.mkdirs();// 目录不存在的情况下，创建目录
			}
			String messageText=message.getMessageText();
			//设置数据是否转换标识,0未处理，1处理中，2处理成功，3处理失败
			message.setIsDeleteFlag(Constant.CONSTANTS_RONGCLOUDCALLBACK_MSG_PARSE_STATE_2);
			if(null!=messageText && !"".equals(messageText))
			{ 
				String  imgUri=message.getMediaUrl();
				String fromUser=String.valueOf(message.getSenderid());
				
			 
				String  ossImgUri=""; 
				if("RC:VcMsg".equals(mediatype))
				{
					String fileName="voice_amr_RC_"+fromUser+"_"+DateUtils.currentDate()+"_"+DateUtils.millis()+".amr";
					String localFilePath=savePath+"/"+fileName;
					String ossFilePath=localFilePath;
					//相对URI
					String DIR =classifyPath+"/"+groupId+"/";
					RongCloudMessageUtil.getVcMsg(messageText, localFilePath);
					logger.fatal("DecodeBase64 finished....................");
					ossImgUri=DIR+fileName;
					//code
					File src = new File(localFilePath);

					File dest = new File(ossFilePath);
					//put oss
					try {
						UploadByOss.upload(src,dest,ossImgUri);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						//设置数据是否转换标识,0未处理，1处理中，2处理成功，3处理失败
						message.setIsDeleteFlag(Constant.CONSTANTS_RONGCLOUDCALLBACK_MSG_PARSE_STATE_3);
						e.printStackTrace();
					}
					
				}
				else if("RC:ImgMsg".equals(mediatype))
				{
					String  fileName="image_jpeg_RC_"+fromUser+"_"+DateUtils.currentDate()+"_"+DateUtils.millis()+".jpeg";
					String localFilePath=savePath+"/"+fileName;
					String ossFilePath=localFilePath;
					//相对URI
					String DIR =classifyPath+"/"+groupId+"/";
					GetRemoteFileHTTPClient.getFile(imgUri, localFilePath);
					logger.fatal("DecodeBase64 finished....................");
					ossImgUri=DIR+fileName;
					//code
					File src = new File(localFilePath);

					File dest = new File(ossFilePath);
					//put oss
					try {
						UploadByOss.upload(src,dest,ossImgUri);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						//设置数据是否转换标识,0未处理，1处理中，2处理成功，3处理失败
						message.setIsDeleteFlag(Constant.CONSTANTS_RONGCLOUDCALLBACK_MSG_PARSE_STATE_3);
						e.printStackTrace();
					}
				}
				message.setMediaUrl(ossImgUri);
			}
 
			messageArray.add(message);
			
		}
	
		
		iMsgIMMessageDao.updateBatch4UploadOSS(messageArray);
		
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateOSSInfo2TempState(List<IMsgIMMessage> messageList) {
		List<IMsgIMMessage> tempMessageArray=  new ArrayList<IMsgIMMessage>();
		for(int i=0;i<messageList.size();i++)
		{
			IMsgIMMessage message = messageList.get(i);
			//设置数据是否转换标识,0未处理，1处理中，2处理成功，3处理失败
			message.setIsDeleteFlag(Constant.CONSTANTS_RONGCLOUDCALLBACK_MSG_PARSE_STATE_1);
			tempMessageArray.add(message);
			 
		}
		iMsgIMMessageDao.updateBatch4UploadOSS(tempMessageArray);
		return 0;
	}
    
    
  
}
